import { BASE_URL } from '../../http/config';

Page({
  data: {
    background: [], // 轮播图数据
    // img:BASE_URL,
    img:'',
   icons: [{
           'image': "/images/@2x_ceping.png",
           'text': "心理测评",
           'url': "/index/pages/psycholigicalTest/psycholigicalTest"
       },
       {
           'image': "/images/@2x_yuyue.png",
           'text': "咨询预约",
           'url': "/index/pages/appointment/appointment"
       },
       {
           'image': "/images/@2x_dayi.png",
           'text': "心理答疑",
           'url': "/index/pages/mentalAnswer/mentalAnswer"
       },
       {
           'image': "/images/@2x_zhishi.png",
           'text': "心理知识",
           'url': "/index/pages/knowledge/knowledge"
       },
       {
           'image': "/images/@2x_FM.png",
           'text': "FM",
           'url': "/index/pages/fm/fm"
       },
       {
           'image': "/images/@2x_gongyi.png",
           'text': "公益中心",
           'url': "/index/pages/publicWelfare/publicWelfare"
       },
   ],
    article: [], // 文章数据
    // img:BASE_URL
  },

    // 获取轮播图数据
    getBanner() {
            wx.request({
                url: `${BASE_URL}/article/findArticlePage`,
                method: 'GET',
                data: {
                    pageNum: 1,
                    pageSize: 6
                },
                success: (res) => {
                    console.log("轮播图数据：", res.data);
                    if (res.data.code === 200) {
                        const rawList = res.data.data.list || [];
                        // 先过滤出status为1的项，再格式化
                        const formattedBanners = rawList
                            .filter(item => item.status === 1) // 只保留status为1的内容
                            .map(item => ({
                                ...item,
                                id: item.id,
                                url: item.url,
                                articleName: item.articleName,
                                articleIntroduction: item.articleIntroduction
                            }));

                        this.setData({
                            background: formattedBanners
                        });
                    }
                },
                fail: (err) => {
                    console.error("获取轮播图失败：", err);
                }
            });
        },

        // 获取全部数据
        getArticle() {
            wx.request({
                url: `${BASE_URL}/article/findArticlePage`,
                method: 'GET',
                data: {
                    pageNum: 1,
                    pageSize: 6
                },
                success: (res) => {
                    console.log("精选文章数据：", res.data);
                    if (res.data.code === 200) {
                        // 先过滤出status为1的项，再格式化
                        const formattedArticles = (res.data.data.list || [])
                            .filter(item => item.status === 1) // 只保留status为1的内容
                            .map(item => ({
                                ...item,
                                id: item.id,
                                name: item.articleName,
                                title: item.articleName,
                                introduction: item.articleIntroduction,
                                listView: item.url
                            }));

                        this.setData({
                            article: formattedArticles
                        });
                    }
                },
                fail: (err) => {
                    console.error("获取文章失败：", err);
                    wx.showToast({
                        title: '加载文章失败',
                        icon: 'none'
                    });
                }
            });
        },
    
  // 跳转客服页面
  jumpIssues() {
    wx.navigateTo({
      url: '/index/pages/kefu/kefu',
    });
  },

  // 跳转功能页面
  jumpUrl(e) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({ url });
  },

  // 生命周期函数
  onLoad: function (options) {
    // 同时获取轮播图和文章数据
    this.getBanner(); // 取消注释以启用轮播图
    this.getArticle();
  },

  // 其他生命周期函数保持不变
});